﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command;
using S.Archives.Infrstructre.InterFace;
using S.Common.EditWanderStateHelper;

namespace S.Archives.Api.Application.CommandHandlers
{
    public class WanderStateEditPLCommandHandler : IRequestHandler<WanderStateEditPLCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public WanderStateEditPLCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }

        public async Task<bool> Handle(WanderStateEditPLCommand request, CancellationToken cancellationToken)
        {
            // 根据传入的羊只耳标列表获取需要修改状态的羊只档案
            var Sheeplist = await sheepFileRepository.GetAllAsync();
            Sheeplist = Sheeplist.Where(x => request.EarTag.Contains(x.EarTag)).ToList();

            if (Sheeplist.Any())
            {
                foreach (var item in Sheeplist)
                {
                    item.WanderReproductiveState = request.WanderReproductiveState;
                    item.UpdateDate=DateTime.Now;
                }
            }
            
           return await sheepFileRepository.UpdateRangeAsync(Sheeplist);
        }
    }
}
